package com.uk.reports;


import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.HashMap;

import net.sf.dynamicreports.jasper.builder.JasperReportBuilder;
import net.sf.dynamicreports.report.exception.DRException;
import net.sf.jasperreports.engine.JRException;
import net.sf.jasperreports.engine.JasperRunManager;

import com.vaadin.Application;
import com.vaadin.terminal.StreamResource;

/**
 * @author hehehe
 *
 */
public class FilePDF implements StreamResource.StreamSource {

    private JasperReportBuilder report;

    //
    public FilePDF(final JasperReportBuilder rpt) {
        report = rpt;
    }

    @Override
    public InputStream getStream() {
        //
        ByteArrayOutputStream os = new ByteArrayOutputStream();

        //
        //os.write(JasperRunManager.runReportToPdf(report.toJasperReport(), new HashMap()));
        try {
            report.toPdf(os);
            try {
                os.flush();
            } catch (IOException e) {
                //
                e.printStackTrace();
            }
        } catch (DRException e) {
            //
            e.printStackTrace();
        }
        return new ByteArrayInputStream(os.toByteArray());
    }

}
